package com.tanhua.controller;

import com.tanhua.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(path = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录-发送验证码
     *请求参数:phone (Map)
     * 响应 ；void
     * ResponseEntity
     * @param param
     * @return
     */
    @PostMapping("login")
    public ResponseEntity login(@RequestBody Map<String, String> param) {
        //1. 获取手机号
        String phone = param.get("phone");
        //2. 发送验证码
        userService.sendCode(phone);
        //3. 返回数据
        return ResponseEntity.ok(null); //ok表示正常返回状态码200
    }

    /**
     * 检验登录
     * /user/loginVerification
     * phone  verificationCode
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){
        // 1  调用map集合获取请求的参数
        String phone = (String) map.get("phone");
        String code= (String) map.get("verificationCode");
        // 2  调用userService完成用户登录
        Map retMap=userService.loginVerification(phone,code);
        //构造返回
        return ResponseEntity.ok(retMap);
    }

}